package com.gulimall.cart.controller;

import com.atguigu.common.utils.R;
import com.gulimall.cart.server.CartService;
import com.gulimall.cart.vo.Cart;
import com.gulimall.cart.vo.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @Description
 * @Author lhj
 * @Date 2024/3/16 14:26
 */
@Slf4j
@Controller
public class CartController {
    
    @Autowired
    CartService cartService;

    /**
     * 获取用户选中购物车
     * @return
     */
    @ResponseBody
    @GetMapping("/getCheckCartItem")
    public R getCheckCatItem() {
        try {
            List<CartItem> cartItems = cartService.getCheckCartItem();
            return R.ok(cartItems);
        } catch (Exception e) {
            log.error("CartController.getCheckCartItem get check cart items exception", e);
            return R.error("获取选中的购物项失败!");
        }
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("delCartItem")
    public String delCartItem(@RequestParam("skuId") Long skuId) {
        cartService.delCartItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    
    /**
     * 修改购物中的数量
     * @param skuId
     * @param cartNum
     * @return
     */
    @GetMapping("cartNum")
    public String cartChangeNum(@RequestParam("skuId") Long skuId, @RequestParam("cartNum") Integer cartNum) {
        cartService.cartChangeNum(skuId, cartNum);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    
    /**
     * 修改购物项选中状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("cartChecked")
    public String cartChecked(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer check) {
        cartService.changeChecked(skuId, check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    
    /**
     * 
     * @return
     */
    @GetMapping("cart.html")
    public String cartListPage(Model model) {
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /** 
     * redirectAttributes.addFlashAttribute("skuId", skuId) : 模拟将请求数据放入到Session中一次
     * redirectAttributes.addAttribute("skuId", skuId): 模拟将数据拼接到请求路径参数上面
     * 添加购物车
     * @param skuId skuId
     * @param cartNum sku数量
     * @param redirectAttributes 域对象
     * @return string
     */
    @GetMapping("addToCart/{skuId}/{cartNum}")
    public String addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("cartNum") Integer cartNum,
                            RedirectAttributes redirectAttributes) {
        cartService.addToCart(skuId, cartNum);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess";
    }

    /**
     * 购物车添加成功
     * @return
     */
    @GetMapping("addToCartSuccess")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cartItem = cartService.getCrtItemBySkuId(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }
    
}
